/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.neusoft.tyut.myspring.myaspect.myaop;

import com.neusoft.tyut.myspring.myaspect.DeptService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

@Component
@Aspect
// 模拟切面
public class Qiemian {
    @Pointcut("execution( * com.neusoft.tyut.myspring.myaspect.*.*(..))")
    //    此方法的作用提供Pointcut的ID
    private void allMethod() {}

    @Around("allMethod()")
    public Object roundTongzhi(ProceedingJoinPoint call) throws Throwable {
        System.out.println("环绕增强前");
        Object retVal = call.proceed();
        System.out.println("环绕增强后");
        return retVal;
    }

    @Order(3) //  不生效，默认值是0，越小优先级越高；方法名决定了优先级
    @Before("allMethod()")
    public void bb() {
        System.out.println("前置增强");
    }

    @AfterReturning(pointcut = "allMethod()", returning = "retVal")
    public void after(Object retVal) {
        System.out.println("返回通知 ，retVal = " + retVal);
    }

    @AfterThrowing(pointcut = "allMethod()", throwing = "ex")
    public void exceptionTongzhi(Exception ex) {
        System.out.println("异常通知，ex = " + ex);
    }

    @Before("allMethod()")
    @Order(2)
    public void aa() {
        System.out.println("前置增强1111111");
    }

    @After("allMethod()")
    public void afterTongzhi() {
        System.out.println("最终增强");
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SimpleDateFormat simpleDateFormat = (SimpleDateFormat) context.getBean("simpleDateFormat");
        System.out.println(simpleDateFormat);
        //        DeptService deptService = context.getBean("deptService", DeptService.class);
        //        deptService.addDept();
    }
}
